package com.css.zfzx.sjcj.modules.classifygroup.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.sjcj.modules.classifygroup.repository.entity.ZfzxClassifyGroupEntity;
import com.css.zfzx.sjcj.modules.classifygroup.service.ZfzxClassifyGroupService;
import com.css.zfzx.sjcj.modules.classifygroup.viewobjects.ZfzxClassifyGroupQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author tao
 * @date 2020-11-06
 */
@Slf4j
@RestController
@RequestMapping("/qh/zfzxClassifyGroups")
public class ZfzxClassifyGroupController {
    @Autowired
    private ZfzxClassifyGroupService zfzxClassifyGroupService;

    @GetMapping("/queryZfzxClassifyGroups")
    public RestResponse queryZfzxClassifyGroups(HttpServletRequest request, ZfzxClassifyGroupQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = zfzxClassifyGroupService.queryZfzxClassifyGroups(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getZfzxClassifyGroup(@PathVariable String id) {
        RestResponse response = null;
        try{
            ZfzxClassifyGroupEntity zfzxClassifyGroup = zfzxClassifyGroupService.getZfzxClassifyGroup(id);
            response = RestResponse.succeed(zfzxClassifyGroup);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveZfzxClassifyGroup(@RequestBody ZfzxClassifyGroupEntity zfzxClassifyGroup) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            zfzxClassifyGroupService.saveZfzxClassifyGroup(zfzxClassifyGroup);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateZfzxClassifyGroup(@RequestBody ZfzxClassifyGroupEntity zfzxClassifyGroup)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            zfzxClassifyGroupService.updateZfzxClassifyGroup(zfzxClassifyGroup);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @DeleteMapping
    public RestResponse deleteZfzxClassifyGroups(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            zfzxClassifyGroupService.deleteZfzxClassifyGroups(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }


}